package net.b521.petshop.service.impl;

import com.google.common.collect.Lists;
import net.b521.petshop.common.ServerResponse;
import net.b521.petshop.dao.SellerMapper;
import net.b521.petshop.dao.model.Seller;
import net.b521.petshop.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service("storeService")
public class StoreServiceImpl implements StoreService {

    @Autowired
    private SellerMapper sellerMapper;

    @Override
    public ServerResponse add(String username,
                              String password,
                              String storeName,
                              String name,
                              String phone,
                              String detail,
                              String email,
                              String addr,
                              HttpServletResponse response) {
        Seller seller = new Seller();
        seller.setStoreName(storeName);
        seller.setDetail(detail);
        seller.setStatus("1");
        int rowCount = sellerMapper.insert(seller);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("开店成功,好运连连，祝你生意兴隆");
        }
        return ServerResponse.createByErrorMessage("开店失败");
    }

    @Override
    public ServerResponse setStoreName(Integer storeId, String storeName) {
        // 检验一下店铺是否存在
        Seller seller = sellerMapper.selectByPrimaryKey(storeId);
        if (null == seller) {
            return ServerResponse.createByErrorMessage("店铺不存在");
        }
        seller.setStoreName(storeName);
        int rowCount = sellerMapper.updateByPrimaryKeySelective(seller);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("店铺名修改成功");
        }
        return ServerResponse.createByErrorMessage("店铺名修改失败");
    }

    @Override
    public ServerResponse closeStore(Integer storeId, Integer sellerId) {
        // 检验一下店铺是否存在
        Seller seller = sellerMapper.selectByPrimaryKey(storeId);
        if (null == seller) {
            return ServerResponse.createByErrorMessage("店铺不存在");
        }
        seller.setStatus("0");
        int rowCount = sellerMapper.updateByPrimaryKeySelective(seller);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("店铺关闭成功");
        }
        return ServerResponse.createByErrorMessage("店铺关闭失败");
    }

    @Override
    public ServerResponse getStoreDetail(Integer storeId, Integer sellerId) {
        // 检验一下店铺是否存在
        Seller seller = sellerMapper.selectByPrimaryKey(storeId);
        if (null == seller) {
            return ServerResponse.createByErrorMessage("店铺不存在");
        }

        return ServerResponse.createBySuccessData(seller);
    }

    @Override
    public ServerResponse listStore() {
        List<Seller> storeList = sellerMapper.selectByExample(null);
        if (storeList!=null) {
            return ServerResponse.createBySuccessData(storeList);
        }
        return ServerResponse.createBySuccessData(Lists.newArrayList());
    }
}
